package 第三章作业;

import java.io.*;

public class Work4_Staff implements Serializable{
    private String name;
    private int age;
    private double salary;

    public Work4_Staff(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public static void saveStaffToFile( Work4_Staff staff  , File file){

        Writer r=null;
        BufferedWriter br=null;
        try {
            r=new FileWriter(file,true);
            br=new BufferedWriter(r);
            String str=staff.getName()+"/"+staff.getAge()+"/"+staff.getSalary();
            br.write(str);
            br.newLine();
            br.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }


    public static void main(String[] args) throws  IOException{
        File file=new File("F:\\java_senior_collection\\doc\\test.txt");
        Work4_Staff w1=new Work4_Staff("Jim",25,3000);
        Work4_Staff w2=new Work4_Staff("Terry",33,4500);
        FileOutputStream fos=null;
        ObjectOutputStream oos=null;

        FileInputStream fis=null;
        ObjectInputStream ois=null;

        try {
            fos=new FileOutputStream("F:\\java_senior_collection\\data\\pet.txt",true);
            oos=new ObjectOutputStream(fos);

            fis=new FileInputStream("F:\\java_senior_collection\\data\\pet.txt");
            ois=new ObjectInputStream(fis);
            oos.writeObject(w1);
            Work4_Staff w11=(Work4_Staff)ois.readObject();
            String s1=w11.getName()+"/"+w11.getAge()+"/"+w11.getSalary();
            oos.writeObject(w2);
            Work4_Staff w22=(Work4_Staff)ois.readObject();
            String s2=w22.getName()+"/"+w22.getAge()+"/"+w22.getSalary();
            System.out.println("序列化成功！");
            System.out.println("反序列化成功！");
            System.out.println(s1+"\n"+s2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Work4_Staff.saveStaffToFile(w1,file);
        Work4_Staff.saveStaffToFile(w2,file);


    }
}


